﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utility
{
    public class ShoppingCart
    {
        private List<CartItem> _cart;

        public ShoppingCart()
        {
            _cart = new List<CartItem>();
        }

        public List<CartItem> GetItems()
        {
            return _cart;
        }

        /// <summary>
        /// add a item
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        public void AddItem(string id, string name, decimal price)
        {
            bool itemFound = false;
            foreach (CartItem item in _cart)
            {
                if (item.ID == id)
                {
                    itemFound = true;
                    item.Quantity += 1;
                }
            }
            if (!itemFound)
            {
                CartItem item = new CartItem(id, name, price, 1);
                _cart.Add(item);
            }
        }


        /// <summary>
        /// update quanity of item
        /// </summary>
        /// <param name="index"></param>
        /// <param name="quantity"></param>
        public void UpdateQuantity(int index, int quantity)
        {
            CartItem item = _cart[index];
            item.Quantity = quantity;
        }

        /// <summary>
        /// delete a item
        /// </summary>
        /// <param name="index"></param>
        public void DeleteItem(int index)
        {
            _cart.RemoveAt(index);

        }


        /// <summary>
        /// int, return quantity of items
        /// </summary>
        public int count
        {
            get { return _cart.Count; }
        }

        public int quantity
        {
            get
            {
                int soluong = 0;
                foreach (CartItem item in _cart)
                {
                    soluong = soluong + item.Quantity;
                }
                return soluong;
            }
        }

        public decimal totalmoney
        {
            get
            {
                decimal money = 0;
                foreach (CartItem item in _cart)
                {
                    money = money + (item.Quantity * item.Price);
                }
                return money;
            }
        }

    }
}
